package com.kwan80gmail.flextime.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.InputType;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.kwan80gmail.flextime.R;
import com.kwan80gmail.flextime.controllers.MainController;
import com.kwan80gmail.flextime.models.GpsTracker;
import com.kwan80gmail.flextime.models.SqlLiteHelper;
import com.kwan80gmail.flextime.models.Place;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity
{
    //DEBUG
    private static final String TAG = "DebugMessage";
    private MainController controller = new MainController();
    TextView locationText;
    Button mainOptionBtn;
    private GpsTracker gps;
    private SqlLiteHelper database;
    private android.location.Location currentLocation;
    private ArrayList<Geofence> mGeofenceList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        database = new SqlLiteHelper(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        locationText = (TextView) findViewById(R.id.locationText);
        mainOptionBtn = (Button) findViewById(R.id.mainOptionBtn);

        locationText.setText("SKALL FIXAS ");

        mainOptionBtn.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v)
            {
                registerForContextMenu(mainOptionBtn);
                return false;
            }
        });

        //Debug
        Log.i(TAG, "onCreate");
        ///////////////////////////////////////
        //currentLocation = gps.getLocation();
        Geofence.Builder builder = new Geofence.Builder();

    }

    private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(mGeofenceList);
        return builder.build();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.setHeaderTitle("Select Option");

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_contextmenu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.optionPause:
                Toast.makeText(getApplicationContext(), "Pause", Toast.LENGTH_LONG).show();
                break;
            case R.id.addCurrentLocation:
                savePlace();
                break;
            case R.id.addLocationByCoordinates:
                Toast.makeText(getApplicationContext(), "Add Place by Coordinates", Toast.LENGTH_LONG).show();
                break;
            case R.id.optionStatistics:
                changeActivity("statisticsActivity");
                Toast.makeText(getApplicationContext(), "Statistics selected", Toast.LENGTH_LONG).show();
                break;
            case R.id.optionLocations:
                changeActivity("placesActivity");
                Toast.makeText(getApplicationContext(), "Places selected", Toast.LENGTH_LONG).show();
                break;
            case R.id.optionMap:
                changeActivity("mapActivity");
                Toast.makeText(getApplicationContext(), "Map selected", Toast.LENGTH_LONG).show();
                break;
        }

        return super.onContextItemSelected(item);
    }

    private void savePlace()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Name Place");

        final EditText input = new EditText(this);

        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);


        builder.setPositiveButton("Save", new DialogInterface.OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {

                gps = controller.getGpsTracker(MainActivity.this);

                if (gps.canGetLocation())
                {
                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();
                    int radius = 100;

                    int id = database.getPlaces().size() + 1;

                    Place place = new Place(id, input.getText().toString(), latitude, longitude, radius);
                    database.insertPlace(place);
                   // controller.getModel().updatePlaces();


                    Toast.makeText(getApplicationContext(), "Saved Place " + input.getText().toString() + "\nLatitdue: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_LONG).show();
                } else
                {
                    gps.showSettingAlert();
                }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.cancel();
            }
        });

        builder.show();


    }

    private void changeActivity(String activityName)
    {
        startActivity(new Intent(activityName));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
